package com.cloudsigns.factorymanager2.engineering.model;


import com.cloudsigns.factorymanager2.MyApplicaton;
import com.cloudsigns.factorymanager2.engineering.bean.EngineeringConstructContentBean;
import com.cloudsigns.factorymanager2.engineering.utils.EngineeringUtil;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;
import com.cloudsigns.factorymanager2.project.utils.ProjectUtil;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.List;

/**
 * 工程施工单进行分派
 */
public class EngineeringConstructOrderAssignSendModel implements IEngineeringConstructOrderAssignSendModel {

    @Override
    public void engineeringConstructOrderAssignSend(
            final List<EngineeringConstructContentBean> engineeringConstructOrderAssignSendItemRequestList, final String id,
            final OnEngineeringConstructOrderAssignSendListener constructOrderAssignSendListener) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                HttpClient client = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(ProjectUtil.ADD_ENGINEERING_CONSTRUCT_ORDER_ASSIGN_SEND);
                HttpResponse response;

                try {
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("id", id);

                    JSONArray constructOrderAssignSendListJsonArr = new JSONArray();
                    if (null != engineeringConstructOrderAssignSendItemRequestList) {
                        for (EngineeringConstructContentBean engineeringConstructContentBean : engineeringConstructOrderAssignSendItemRequestList) {
                            JSONObject jsonObj = new JSONObject();
                            jsonObj.put("id", engineeringConstructContentBean.getId());
                            jsonObj.put("receiveAccountId", engineeringConstructContentBean.getAssignReceiveAccountId());
                            constructOrderAssignSendListJsonArr.put(jsonObj);
                        }
                    }

                    jsonObject.put("engineeringConstructOrderAssignSendItemRequestList", constructOrderAssignSendListJsonArr);

                    StringEntity se = new StringEntity(jsonObject.toString(), HTTP.UTF_8);
                    httpPost.addHeader(HTTP.CONTENT_TYPE, ConstantUtil.CONTENT_TYPE);
                    httpPost.addHeader("token", MyApplicaton.getApplication().getToken());
                    httpPost.setEntity(se);

                    response = client.execute(httpPost);
                    String result = EntityUtils.toString(response.getEntity(), "UTF-8").trim();
                    JSONObject json = new JSONObject(result);
                    String code = json.getString("code");

                    if (null != constructOrderAssignSendListener) {
                        if (code.equals(EngineeringUtil.HTTP_SUCCESSED_COD)) {

                            constructOrderAssignSendListener.success(json.getString("id"));
                        } else {
                            String error_str = code;
                            error_str += "," + ConstantUtil.getErrorCode(code);
                            if (!json.isNull("error_str")) {
                                error_str += "," + json.getString("error_str");
                            }
                            constructOrderAssignSendListener.failed(error_str);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    public interface OnEngineeringConstructOrderAssignSendListener {
        void success(String id);

        void failed(String desc);
    }
}
